package com.ruoyi.drive.service.Impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.drive.domain.Driver;
import com.ruoyi.drive.mapper.DriverMapper;
import com.ruoyi.drive.service.DriverService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements DriverService{
    // Token过期时间(分钟)
    private static final long TOKEN_EXPIRE_TIME = CacheConstants.EXPIRATION;
    
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public Map<String, Object> createToken(Driver driver1) {
        String token = IdUtils.fastUUID();
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put(SecurityConstants.USER_KEY, token);
        stringObjectHashMap.put(SecurityConstants.DETAILS_USER_ID, driver1.getDriverId());
        stringObjectHashMap.put(SecurityConstants.DETAILS_USERNAME, driver1.getName());
        String tokenKey = "driverToken:" + token;
        
        // 将用户信息存储到Redis中
        redisTemplate.opsForValue().set(tokenKey, stringObjectHashMap.toString(), TOKEN_EXPIRE_TIME, TimeUnit.MINUTES);
        
        Map<String, Object> stringObjectHashMap1 = new HashMap<>();
        stringObjectHashMap1.put("access_token", JwtUtils.createToken(stringObjectHashMap));
        stringObjectHashMap1.put("expires_in", TOKEN_EXPIRE_TIME); // 直接使用分钟单位
        return stringObjectHashMap1;
    }


}
